package com.service;

import com.entity.Student;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl extends BaseService {
    public StudentServiceImpl() throws SQLException {
    }

    public void addStudent(Student student) throws SQLException {
        Connection conn = this.getConn();
        try (Statement stmt = conn.createStatement()) {
            stmt.executeUpdate("insert into student(id,name,sex,birthday,politicalstatus,address,phone,dormnum)" +
                    " values('" + student.getId() +
                    "','" + student.getName() +
                    "','" + student.getSex() +
                    "','" + student.getBirthday() +
                    "','" + student.getPoliticalstatus() +
                    "','" + student.getAddress() + "" +
                    "','" + student.getPhone() + "" +
                    "','" + student.getDormnum() + "')");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("添加请求请求失败");
        }
    }

    public List<Student> getAllStudents() throws SQLException {
        //throw new SQLException("获取学生列表失败");
        List<Student> students = new ArrayList<>();

        Connection conn = this.getConn();
        try (Statement stmt = conn.createStatement()) {

            ResultSet rs = stmt.executeQuery("SELECT * FROM student");
            while (rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String birthday = rs.getString("birthday");
                String politicalstatus = rs.getString("politicalstatus");//随便怎么输入都可以
                String address = rs.getString("address");//随便怎么输入都可以
                String dormnum = rs.getString("dormnum");//随便怎么输入都可以
                String phone = rs.getString("phone");//随便怎么输入都可以

                Student student = new Student(id,name,sex,birthday,politicalstatus,address,phone,dormnum);
                students.add(student);
            }

        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("获取学生列表请求请求失败");
        }

        return students;
    }

    public void updateStudent(Student student) throws SQLException{
        Connection conn = this.getConn();
        try (Statement stmt = conn.createStatement()) {
            stmt.executeUpdate("UPDATE student " +
                    " set name='" + student.getName() +
                    "',sex='" + student.getSex() +
                    "',birthday='" + student.getBirthday() +
                    "',politicalstatus='" + student.getPoliticalstatus() +
                    "',address='" + student.getAddress() + "" +
                    "',phone='" + student.getPhone() + "" +
                    "',dormnum='" + student.getDormnum() +
                    "' WHERE id='"+student.getId()+"'");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("修改学生请求失败");
        }
    }

    public void deleteStudent(String id) throws SQLException{
        Connection conn = this.getConn();
        try (Statement stmt = conn.createStatement()) {
            stmt.executeUpdate("DELETE FROM student WHERE id='"+id+"'");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("删除学生请求失败");
        }
    }
}
